home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 85 / CD Actual 85 Febrero 2004.iso / Experto / Apache / apache_2.0.48-win32-x86-no_ssl.msi / Data.Cab / F251733_apr_atomic.h < prev    next >
Encoding:
C/C++ Source or Header  |  2003-09-19  |  12.8 KB  |  358 lines

  1. /* ====================================================================
  2.  * The Apache Software License, Version 1.1
  3.  *
  4.  * Copyright (c) 2000-2003 The Apache Software Foundation.  All rights
  5.  * reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  *
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  *
  14.  * 2. Redistributions in binary form must reproduce the above copyright
  15.  *    notice, this list of conditions and the following disclaimer in
  16.  *    the documentation and/or other materials provided with the
  17.  *    distribution.
  18.  *
  19.  * 3. The end-user documentation included with the redistribution,
  20.  *    if any, must include the following acknowledgment:
  21.  *       "This product includes software developed by the
  22.  *        Apache Software Foundation (http://www.apache.org/)."
  23.  *    Alternately, this acknowledgment may appear in the software itself,
  24.  *    if and wherever such third-party acknowledgments normally appear.
  25.  *
  26.  * 4. The names "Apache" and "Apache Software Foundation" must
  27.  *    not be used to endorse or promote products derived from this
  28.  *    software without prior written permission. For written
  29.  *    permission, please contact apache@apache.org.
  30.  *
  31.  * 5. Products derived from this software may not be called "Apache",
  32.  *    nor may "Apache" appear in their name, without prior written
  33.  *    permission of the Apache Software Foundation.
  34.  *
  35.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36.  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38.  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39.  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42.  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44.  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45.  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46.  * SUCH DAMAGE.
  47.  * ====================================================================
  48.  *
  49.  * This software consists of voluntary contributions made by many
  50.  * individuals on behalf of the Apache Software Foundation.  For more
  51.  * information on the Apache Software Foundation, please see
  52.  * <http://www.apache.org/>.
  53.  *
  54.  * Portions of this software are based upon public domain software
  55.  * originally written at the National Center for Supercomputing Applications,
  56.  * University of Illinois, Urbana-Champaign.
  57.  */
  58.  
  59. #ifndef APR_ATOMIC_H
  60. #define APR_ATOMIC_H
  61.  
  62. /**
  63.  * @file apr_atomic.h
  64.  * @brief APR Atomic Operations
  65.  */
  66.  
  67. #include "apr.h"
  68. #include "apr_pools.h"
  69.  
  70. /* Platform includes for atomics */
  71. #if defined(NETWARE) || defined(__MVS__) /* OS/390 */
  72. #include <stdlib.h>
  73. #elif defined(__FreeBSD__)
  74. #include <machine/atomic.h>
  75. #endif
  76.  
  77. #ifdef __cplusplus
  78. extern "C" {
  79. #endif
  80.  
  81. /**
  82.  * @defgroup apr_atomic Atomic Operations
  83.  * @ingroup APR 
  84.  * @{
  85.  */
  86.  
  87. /* easiest way to get these documented for the moment */
  88. #if defined(DOXYGEN)
  89. /**
  90.  * structure for holding a atomic value.
  91.  * this number >only< has a 24 bit size on some platforms
  92.  */
  93. typedef apr_atomic_t;
  94.  
  95. /**
  96.  * this function is required on some platforms to initialize the
  97.  * atomic operation's internal structures
  98.  * @param p pool
  99.  * @return APR_SUCCESS on successful completion
  100.  */
  101. apr_status_t apr_atomic_init(apr_pool_t *p);
  102. /**
  103.  * read the value stored in a atomic variable
  104.  * @param mem the pointer
  105.  * @warning on certain platforms this number is not stored
  106.  * directly in the pointer. in others it is 
  107.  */
  108. apr_uint32_t apr_atomic_read(volatile apr_atomic_t *mem);
  109. /**
  110.  * set the value for atomic.
  111.  * @param mem the pointer
  112.  * @param val the value
  113.  */
  114. void apr_atomic_set(volatile apr_atomic_t *mem, apr_uint32_t val);
  115. /**
  116.  * Add 'val' to the atomic variable
  117.  * @param mem pointer to the atomic value
  118.  * @param val the addition
  119.  */
  120. void apr_atomic_add(volatile apr_atomic_t *mem, apr_uint32_t val);
  121.  
  122. /**
  123.  * increment the atomic variable by 1
  124.  * @param mem pointer to the atomic value
  125.  */
  126. void apr_atomic_inc(volatile apr_atomic_t *mem);
  127.  
  128. /**
  129.  * decrement the atomic variable by 1
  130.  * @param mem pointer to the atomic value
  131.  * @return zero if the value is zero, otherwise non-zero
  132.  */
  133. int apr_atomic_dec(volatile apr_atomic_t *mem);
  134.  
  135. /**
  136.  * compare the atomic's value with cmp.
  137.  * If they are the same swap the value with 'with'
  138.  * @param mem pointer to the atomic value
  139.  * @param with what to swap it with
  140.  * @param cmp the value to compare it to
  141.  * @return the old value of the atomic
  142.  * @warning do not mix apr_atomic's with the CAS function.
  143.  * on some platforms they may be implemented by different mechanisms
  144.  */
  145. apr_uint32_t apr_atomic_cas(volatile apr_uint32_t *mem, long with, long cmp);
  146.  
  147. /**
  148.  * compare the pointer's value with cmp.
  149.  * If they are the same swap the value with 'with'
  150.  * @param mem pointer to the pointer
  151.  * @param with what to swap it with
  152.  * @param cmp the value to compare it to
  153.  * @return the old value of the pointer
  154.  */
  155. void *apr_atomic_casptr(volatile void **mem, void *with, const void *cmp);
  156. #else /* !DOXYGEN */
  157.  
  158. /* The following definitions provide optimized, OS-specific
  159.  * implementations of the APR atomic functions on various
  160.  * platforms.  Any atomic operation that isn't redefined as
  161.  * a macro here will be declared as a function later, and
  162.  * apr_atomic.c will provide a mutex-based default implementation.
  163.  */
  164.  
  165. #if defined(WIN32)
  166.  
  167. #define apr_atomic_t LONG
  168.  
  169. #define apr_atomic_add(mem, val)     InterlockedExchangeAdd(mem,val)
  170. #define apr_atomic_dec(mem)          InterlockedDecrement(mem)
  171. #define apr_atomic_inc(mem)          InterlockedIncrement(mem)
  172. #define apr_atomic_set(mem, val)     InterlockedExchange(mem, val)
  173. #define apr_atomic_read(mem)         (*mem)
  174. #define apr_atomic_cas(mem,with,cmp) InterlockedCompareExchange(mem,with,cmp)
  175. #define apr_atomic_init(pool)        APR_SUCCESS
  176. #define apr_atomic_casptr(mem,with,cmp) InterlockedCompareExchangePointer(mem,with,cmp)
  177.  
  178. #elif defined(NETWARE)
  179.  
  180. #define apr_atomic_t unsigned long
  181.  
  182. #define apr_atomic_add(mem, val)     atomic_add(mem,val)
  183. #define apr_atomic_inc(mem)          atomic_inc(mem)
  184. #define apr_atomic_set(mem, val)     (*mem = val)
  185. #define apr_atomic_read(mem)         (*mem)
  186. #define apr_atomic_init(pool)        APR_SUCCESS
  187. #define apr_atomic_cas(mem,with,cmp) atomic_cmpxchg((unsigned long *)(mem),(unsigned long)(cmp),(unsigned long)(with))
  188.     
  189. int apr_atomic_dec(apr_atomic_t *mem);
  190. void *apr_atomic_casptr(void **mem, void *with, const void *cmp);
  191. #define APR_OVERRIDE_ATOMIC_DEC 1
  192. #define APR_OVERRIDE_ATOMIC_CASPTR 1
  193.  
  194. inline int apr_atomic_dec(apr_atomic_t *mem) 
  195. {
  196.     atomic_dec(mem);
  197.     return *mem; 
  198. }
  199.  
  200. inline void *apr_atomic_casptr(void **mem, void *with, const void *cmp)
  201. {
  202.     return (void*)atomic_cmpxchg((unsigned long *)mem,(unsigned long)cmp,(unsigned long)with);
  203. }
  204.  
  205. #elif defined(__FreeBSD__)
  206.  
  207. #define apr_atomic_t apr_uint32_t
  208. #define apr_atomic_add(mem, val)     atomic_add_int(mem,val)
  209. #define apr_atomic_dec(mem)          atomic_subtract_int(mem,1)
  210. #define apr_atomic_inc(mem)          atomic_add_int(mem,1)
  211. #define apr_atomic_set(mem, val)     atomic_set_int(mem, val)
  212. #define apr_atomic_read(mem)         (*mem)
  213.  
  214. #elif (defined(__linux__) || defined(__EMX__)) && defined(__i386__) && !APR_FORCE_ATOMIC_GENERIC
  215.  
  216. #define apr_atomic_t apr_uint32_t
  217. #define apr_atomic_cas(mem,with,cmp) \
  218. ({ apr_atomic_t prev; \
  219.     asm volatile ("lock; cmpxchgl %1, %2"              \
  220.          : "=a" (prev)               \
  221.          : "r" (with), "m" (*(mem)), "0"(cmp) \
  222.          : "memory"); \
  223.     prev;})
  224.  
  225. #define apr_atomic_add(mem, val)                                \
  226. ({ register apr_atomic_t last;                                  \
  227.    do {                                                         \
  228.        last = *(mem);                                           \
  229.    } while (apr_atomic_cas((mem), last + (val), last) != last); \
  230.   })
  231.  
  232. #define apr_atomic_dec(mem)                                     \
  233. ({ register apr_atomic_t last;                                  \
  234.    do {                                                         \
  235.        last = *(mem);                                           \
  236.    } while (apr_atomic_cas((mem), last - 1, last) != last);     \
  237.   (--last != 0); })
  238.  
  239. #define apr_atomic_inc(mem)                                     \
  240. ({ register apr_atomic_t last;                                  \
  241.    do {                                                         \
  242.        last = *(mem);                                           \
  243.    } while (apr_atomic_cas((mem), last + 1, last) != last);     \
  244.   })
  245.  
  246. #define apr_atomic_set(mem, val)     (*(mem) = val)
  247. #define apr_atomic_read(mem)        (*(mem))
  248. #define apr_atomic_init(pool)        APR_SUCCESS
  249.  
  250. #elif defined(__MVS__) /* OS/390 */
  251.  
  252. #define apr_atomic_t cs_t
  253.  
  254. apr_int32_t apr_atomic_add(volatile apr_atomic_t *mem, apr_int32_t val);
  255. apr_uint32_t apr_atomic_cas(volatile apr_atomic_t *mem, apr_uint32_t swap, 
  256.                             apr_uint32_t cmp);
  257. #define APR_OVERRIDE_ATOMIC_ADD 1
  258. #define APR_OVERRIDE_ATOMIC_CAS 1
  259.  
  260. #define apr_atomic_inc(mem)          apr_atomic_add(mem, 1)
  261. #define apr_atomic_dec(mem)          apr_atomic_add(mem, -1)
  262. #define apr_atomic_init(pool)        APR_SUCCESS
  263.  
  264. /* warning: the following two operations, _read and _set, are atomic
  265.  * if the memory variables are aligned (the usual case).  
  266.  * 
  267.  * If you try really hard and manage to mis-align them, they are not 
  268.  * guaranteed to be atomic on S/390.  But then your program will blow up 
  269.  * with SIGBUS on a sparc, or with a S0C6 abend if you use the mis-aligned 
  270.  * variables with other apr_atomic_* operations on OS/390.
  271.  */
  272.  
  273. #define apr_atomic_read(p)           (*p)
  274. #define apr_atomic_set(mem, val)     (*mem = val)
  275.  
  276. #endif /* end big if-elseif switch for platform-specifics */
  277.  
  278.  
  279. /* Default implementation of the atomic API
  280.  * The definitions above may override some or all of the
  281.  * atomic functions with optimized, platform-specific versions.
  282.  * Any operation that hasn't been overridden as a macro above
  283.  * is declared as a function here, unless APR_OVERRIDE_ATOMIC_[OPERATION]
  284.  * is defined.  (The purpose of the APR_OVERRIDE_ATOMIC_* is
  285.  * to allow a platform to declare an apr_atomic_*() function
  286.  * with a different signature than the default.)
  287.  */
  288.  
  289. #if !defined(apr_atomic_t)
  290. #define apr_atomic_t apr_uint32_t
  291. #endif
  292.  
  293. #if !defined(apr_atomic_init) && !defined(APR_OVERRIDE_ATOMIC_INIT)
  294. apr_status_t apr_atomic_init(apr_pool_t *p);
  295. #endif
  296.  
  297. #if !defined(apr_atomic_read) && !defined(APR_OVERRIDE_ATOMIC_READ)
  298. #define apr_atomic_read(p)  *p
  299. #endif
  300.  
  301. #if !defined(apr_atomic_set) && !defined(APR_OVERRIDE_ATOMIC_SET)
  302. void apr_atomic_set(volatile apr_atomic_t *mem, apr_uint32_t val);
  303. #define APR_ATOMIC_NEED_DEFAULT_INIT 1
  304. #endif
  305.  
  306. #if !defined(apr_atomic_add) && !defined(APR_OVERRIDE_ATOMIC_ADD)
  307. void apr_atomic_add(volatile apr_atomic_t *mem, apr_uint32_t val);
  308. #define APR_ATOMIC_NEED_DEFAULT_INIT 1
  309. #endif
  310.  
  311. #if !defined(apr_atomic_inc) && !defined(APR_OVERRIDE_ATOMIC_INC)
  312. void apr_atomic_inc(volatile apr_atomic_t *mem);
  313. #define APR_ATOMIC_NEED_DEFAULT_INIT 1
  314. #endif
  315.  
  316. #if !defined(apr_atomic_dec) && !defined(APR_OVERRIDE_ATOMIC_DEC)
  317. int apr_atomic_dec(volatile apr_atomic_t *mem);
  318. #define APR_ATOMIC_NEED_DEFAULT_INIT 1
  319. #endif
  320.  
  321. #if !defined(apr_atomic_cas) && !defined(APR_OVERRIDE_ATOMIC_CAS)
  322. apr_uint32_t apr_atomic_cas(volatile apr_uint32_t *mem,long with,long cmp);
  323. #define APR_ATOMIC_NEED_DEFAULT_INIT 1
  324. #endif
  325.  
  326. #if !defined(apr_atomic_casptr) && !defined(APR_OVERRIDE_ATOMIC_CASPTR)
  327. #if APR_SIZEOF_VOIDP == 4
  328. #define apr_atomic_casptr(mem, with, cmp) (void *)apr_atomic_cas((apr_uint32_t *)(mem), (long)(with), (long)cmp)
  329. #else
  330. void *apr_atomic_casptr(volatile void **mem, void *with, const void *cmp);
  331. #define APR_ATOMIC_NEED_DEFAULT_INIT 1
  332. #endif
  333. #endif
  334.  
  335. #ifndef APR_ATOMIC_NEED_DEFAULT_INIT
  336. #define APR_ATOMIC_NEED_DEFAULT_INIT 0
  337. #endif
  338.  
  339. /* If we're using the default versions of any of the atomic functions,
  340.  * we'll need the atomic init to set up mutexes.  If a platform-specific
  341.  * override above has replaced the atomic_init with a macro, it's an error.
  342.  */
  343. #if APR_ATOMIC_NEED_DEFAULT_INIT
  344. #if defined(apr_atomic_init) || defined(APR_OVERRIDE_ATOMIC_INIT)
  345. #error Platform has redefined apr_atomic_init, but other default default atomics require a default apr_atomic_init
  346. #endif
  347. #endif /* APR_ATOMIC_NEED_DEFAULT_INIT */
  348.  
  349. #endif /* !DOXYGEN */
  350.  
  351. /** @} */
  352.  
  353. #ifdef __cplusplus
  354. }
  355. #endif
  356.  
  357. #endif    /* !APR_ATOMIC_H */
  358.